#include <stdio.h>
#include <stdlib.h>

int main()
{
    int w, i, j, k;
    int a;
    int m, n;
    char *week="Su Mo Tu We Th Fr Sa";
    char *month[13]={"","      January       ",
                        "      February      ",
                        "        March       ",
                        "        April       ",
                        "         May        ",
                        "        June        ",
                        "        July        ",
                        "       August       ",
                        "      September     ",
                        "      October       ",
                        "      November      ",
                        "      December      "};
    int days[13][60]={0};
    int month_end[13]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    int year=2016;
    if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) /*闰年的二月为29天*/
    {
        month_end[2] = 29;
    }
    w = ((year-1) + (year)/4 - (year)/100 + (year)/400) % 7; /*计算y年元旦为星期w*/
    printf("%36d\n", year); /*打印年号*/
    for(i = 1; i < 13; i ++)
    {
        a = 1;
        for(j = 1; j <=6; j ++)
        {
            for(k = 0; k <=6; k ++)
            {
                while(k < w) k ++;
                days[i][j * 7 + k] = a; /*计算i月的第j个星期的星期w的日期为a*/
                a ++;
                w ++;
                if(w == 7)w = 0;
                if(a > month_end[i])break;
            }
            if(a > month_end[i])break;
        }
    }
    for(n = 1; n <= 4; n ++)
    {
        for(m = (n - 1) * 3 + 1; m <= 3 * n; m ++)/*打印月号*/
            printf("%-20s\t", month[m]);
        printf("\n");
        for(m = 1; m <=3; m ++)/*打印星期标题*/
            printf("%s\t", week);
        printf("\n");
        for(j = 1; j <= 6; j ++)
        {
            for(i = (n - 1) * 3 + 1; i <= n * 3; i ++)
            {
                for(k = 0; k <=6; k ++)
                {
                    if(days[i][j * 7 + k] == 0)
                        if(k == 0)/*空缺日期位置打印空格*/
                            printf("%2c", ' ');
                        else
                            printf("%3c", ' ');
                    else/*打印日期*/
                        if(k == 0)
                            printf("%2d", days[i][j * 7 + k]);
                        else
                            printf("%3d", days[i][j * 7 + k]);
                }
                printf("\t");
            }
            printf("\n");
        }
        printf("\n");
    }
}
